package org.finesys.common.security.client.config;


import org.finesys.common.security.client.http.GiteeAuthorizeHttpFilter;
import org.finesys.common.security.client.http.GiteeCodeHttpFilter;
import org.finesys.common.security.client.properties.GiteeProperties;
import org.finesys.common.security.client.service.GiteeRegisteredClientRepository;
import org.finesys.common.security.client.service.impl.DefaultGiteeRegistererClientRepository;
import org.finesys.system.api.service.UserGiteeService;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;

import java.util.Arrays;
import java.util.List;

@SuppressWarnings("all")
@EnableConfigurationProperties(GiteeProperties.class)
@Configuration
public class GiteeOAuth2ClientConfigure {

    @Bean
    public DefaultGiteeRegistererClientRepository giteeRegisteredClientRepository(GiteeProperties giteeProperties, StringRedisTemplate redisTemplate, UserGiteeService userGiteeService) {
        return new DefaultGiteeRegistererClientRepository(giteeProperties, redisTemplate, userGiteeService);
    }

    @Bean
    public FilterRegistrationBean<GiteeAuthorizeHttpFilter> giteeAuthorizeHttpFilterFilterRegistrationBean(GiteeRegisteredClientRepository giteeRegisteredClientRepository, OAuth2AuthorizationService oAuth2AuthorizationService) {
        FilterRegistrationBean<GiteeAuthorizeHttpFilter> registrationBean = new FilterRegistrationBean<GiteeAuthorizeHttpFilter>();
        GiteeAuthorizeHttpFilter giteeAuthorizeHttpFilter = new GiteeAuthorizeHttpFilter(giteeRegisteredClientRepository, oAuth2AuthorizationService);
        registrationBean.setFilter(giteeAuthorizeHttpFilter);
        List<String> urlPatterns = Arrays.asList("/gitee/authorize/*");
        registrationBean.setUrlPatterns(urlPatterns);
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean<GiteeCodeHttpFilter> giteeCodeHtppFilterFilterRegistrationBean(GiteeRegisteredClientRepository giteeRegisteredClientRepository) {
        FilterRegistrationBean<GiteeCodeHttpFilter> registrationBean = new FilterRegistrationBean<GiteeCodeHttpFilter>();
        registrationBean.setFilter(new GiteeCodeHttpFilter(giteeRegisteredClientRepository));
        List<String> urlPatterns = Arrays.asList("/gitee/code/*");
        registrationBean.setUrlPatterns(urlPatterns);
        return registrationBean;
    }
}
